﻿package we3d.math 
{
	import we3d.mesh.Vertex;
	
	public class Plane 
	{
		public function Plane (ax:Number=0, ay:Number=0, az:Number=0, aw:Number=0) {
			x = ax;
			y = ay;
			z = az;
			w = aw;
		}
		
		public var x:Number;
		public var y:Number;
		public var z:Number;
		public var w:Number;
		
		public function create (a:Vertex, b:Vertex, c:Vertex) :void {
			var x1:Number = b.x-a.x;
			var y1:Number = b.y-a.y;
			var z1:Number = b.z-a.z;
			
			var x2:Number = c.x-a.x;
			var y2:Number = c.y-a.y;
			var z2:Number = c.z-a.z;
			
			x = y1 * z2 - z1 * y2;
			y = z1 * x2 - x1 * z2;
			z = x1 * y2 - y1 * x2;
			
			var m:Number = Math.sqrt(x*x + y*y + z*z);
			x /= m;
			y /= m;
			z /= m;
			
			w = x * a.x + y * a.y + z * a.z;
		}
		
	}
}